package cn.bytecollege.service;

import cn.bytecollege.mapper.EvaluationMapper;
import cn.bytecollege.mapper.GoodsMapper;
import cn.bytecollege.model.Evaluation;
import cn.bytecollege.model.Goods;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private EvaluationMapper evaluationMapper;
    public Goods getById(Integer id) {
        return goodsMapper.selectById(id);
    }

    /**
     * update goods stock
     * @param goodsId
     * @param buyNumber
     * @return mapper result
     */
    public Integer modGoodsStock(Integer goodsId,Integer buyNumber) {
        Goods goods = getById(goodsId);
        Integer goodsStock = goods.getGoodsStock();
        goods.setGoodsStock(goodsStock-buyNumber);
        return goodsMapper.updateById(goods);
    }

    public List<Evaluation> getEvaluationById(Integer evalId) {
        Map<String, Object> map = new HashMap<>();
        map.put("goods_id", evalId);
        return evaluationMapper.selectByMap(map);
    }


    public List<Goods> getGoodsByKind(String goodsKind) {
        Map<String, Object> map = new HashMap<>();
        map.put("goods_kind", goodsKind);
        return goodsMapper.selectByMap(map);
    }
    public List<Goods> getLikeName(String name) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<Goods>().like("goods_name", name);
        return goodsMapper.selectList(queryWrapper);
    }
}
